package com.pwc.conversionTool.dataAdaptor;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	//in applicant.xml we current only support "yyyy-MM-dd-HH:mm" and "yyyy-MM-dd+HH:mm" and "yyyy-MM-dd"..we will return date error for other format.
	
		public static String addDay(String getDateTime,String days) {
			if(!"".equals(days)){
				int daysN=Integer.parseInt(days);
				String dateFormat=getDateFormat(getDateTime);
				SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
				Date date=new Date();
				try {
					date=bartDateFormat.parse(getDateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					return "Date Error";
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				cal.add(Calendar.DATE, daysN);
				return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
			
		} 
		
		public static String minusDay(String getDateTime,String days) {
			if(!"".equals(days)){
				int daysN=Integer.parseInt(days);
				String dateFormat=getDateFormat(getDateTime);
				SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
				Date date=new Date();
				try {
					date=bartDateFormat.parse(getDateTime);
				} catch (ParseException e) {
					// TODO Auto-generated catch block
					return "Date Error";
				}
				Calendar cal = Calendar.getInstance();
				cal.setTime(date);
				cal.add(Calendar.DATE, -daysN);
				return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
			
		} 
		
		public static String addMonth(String getDateTime,String Months) {
			if(!"".equals(Months)){
			int monthsN=Integer.parseInt(Months);
			String dateFormat=getDateFormat(getDateTime);
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
			Date date=new Date();
			try {
				date=bartDateFormat.parse(getDateTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return "Date Error";
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, monthsN);
			
			    return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
		}
		
		public static String minusMonth(String getDateTime,String Months) {
			if(!"".equals(Months)){
			int monthsN=Integer.parseInt(Months);
			String dateFormat=getDateFormat(getDateTime);
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
			Date date=new Date();
			try {
				date=bartDateFormat.parse(getDateTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return "Date Error";
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.MONTH, -monthsN);
			
			    return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
		}
		
		public static String addYear(String getDateTime,String Years) {
			if(!"".equals(Years)){
			int yearsN=Integer.parseInt(Years);
			String dateFormat=getDateFormat(getDateTime);
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
			Date date=new Date();
			try {
				date=bartDateFormat.parse(getDateTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return "Date Error";
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.YEAR, yearsN);
			
			    return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
		}
		
		public static String minusYear(String getDateTime,String Years) {
			if(!"".equals(Years)){
			int yearsN=Integer.parseInt(Years);
			String dateFormat=getDateFormat(getDateTime);
			SimpleDateFormat bartDateFormat = new SimpleDateFormat(dateFormat);
			Date date=new Date();
			try {
				date=bartDateFormat.parse(getDateTime);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				return "Date Error";
			}
			Calendar cal = Calendar.getInstance();
			cal.setTime(date);
			cal.add(Calendar.YEAR, -yearsN);
			
			    return bartDateFormat.format(cal.getTime());
			}else{
				return getDateTime;
			}
		}
		
		public static String getCurrentTime(){
			
			
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd");
			Calendar cal = Calendar.getInstance();
			 
			
			return bartDateFormat.format(cal.getTime());
		}
		
		public static String getCurrentTimeForXML(){
			
			
			SimpleDateFormat bartDateFormat = new SimpleDateFormat("yyyy-MM-dd-HH-mm");
			Calendar cal = Calendar.getInstance();
			 
			
			return bartDateFormat.format(cal.getTime());
		}

		//stupid method,only support applicant currently................
		public static String getDateFormat(String getDateTime){
			String dateFormat="yyyy-MM-dd";
			if(getDateTime.contains("-")&&getDateTime.contains(":")){
				dateFormat= "yyyy-MM-dd-HH:mm";
			}
			if(getDateTime.contains("+")){
				dateFormat= "yyyy-MM-dd+HH:mm";
			}
			return dateFormat;
		}

}
